package com.mall.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.*;
import com.mall.dto.PmsProductDto;
import com.mall.pojo.*;
import com.mall.vo.PmsProductVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductService {

    @DubboReference
    private PmsProductApi pmsProductApi;
    @DubboReference
    private PmsMemberPriceApi pmsMemberPriceApi;
    @DubboReference
    private CmsPrefrenceAreaProductRelationApi cmsPrefrenceAreaProductRelationApi;
    @DubboReference
    private PmsProductAttributeValueApi pmsProductAttributeValueApi;
    @DubboReference
    private PmsProductFullReductionApi pmsProductFullReductionApi;
    @DubboReference
    private PmsProductLadderApi pmsProductLadderApi;
    @DubboReference
    private PmsSkuStockApi pmsSkuStockApi;
    @DubboReference
    private CmsSubjectProductRelationApi cmsSubjectProductRelationApi;

    //查询商品
    public Page<PmsProduct> list(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        return pmsProductApi.list(brandId, keyword, pageNum, pageSize, productCategoryId, productSn, publishStatus, verifyStatus);
    }

    //根据商品名称或货号模糊查询
    public List<PmsProduct> simpleList(String keyword) {
        return pmsProductApi.simpleList(keyword);
    }

    //批量修改删除状态
    public void deleteStatus(List<Integer> ids, Integer deleteStatus) {
        pmsProductApi.deleteStatus(ids, deleteStatus);
    }

    //批量设为新品
    public void newStatus(List<Integer> ids, Integer newStatus) {
        pmsProductApi.newStatus(ids, newStatus);
    }

    //批量上下架商品
    public void publishStatus(List<Integer> ids, Integer publishStatus) {
        pmsProductApi.publishStatus(ids, publishStatus);
    }

    //批量推荐商品
    public void recommendStatus(List<Integer> ids, Integer recommendStatus) {
        pmsProductApi.recommendStatus(ids, recommendStatus);
    }

    //批量修改审核状态
    public void verifyStatus(List<Integer> ids, String detail, Integer verifyStatus) {
        pmsProductApi.verifyStatus(ids, detail, verifyStatus);
    }

    //创建商品
    //想法 这个接口涉及八张表 在此处分别往八个接口送所需要的数据进行创建
    //todo 待会测试记得分别看看八个接口数据是否正常
    public void create(PmsProductDto pmsProductDto) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(pmsProductDto, pmsProduct);
        pmsProductApi.create(pmsProduct);
        pmsMemberPriceApi.create(pmsProductDto.getMemberPriceList());
        cmsPrefrenceAreaProductRelationApi.create(pmsProductDto.getPrefrenceAreaProductRelationList());
        pmsProductAttributeValueApi.cteate(pmsProductDto.getProductAttributeValueList());
        pmsProductFullReductionApi.create(pmsProductDto.getProductFullReductionList());
        pmsProductLadderApi.create(pmsProductDto.getProductLadderList());
        pmsSkuStockApi.create(pmsProductDto.getPmsSkuStockList());
        cmsSubjectProductRelationApi.create(pmsProductDto.getSubjectProductRelationList());
    }

    //更新商品 更新八张表
    public void update(Integer id, PmsProductDto pmsProductDto) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(pmsProductDto, pmsProduct);
        pmsProductApi.update(id, pmsProduct);
        pmsMemberPriceApi.update(pmsProductDto.getMemberPriceList());
        cmsPrefrenceAreaProductRelationApi.update(pmsProductDto.getPrefrenceAreaProductRelationList());
        pmsProductAttributeValueApi.update(pmsProductDto.getProductAttributeValueList());
        pmsProductFullReductionApi.update(pmsProductDto.getProductFullReductionList());
        pmsProductLadderApi.update(pmsProductDto.getProductLadderList());
        pmsSkuStockApi.updates(pmsProductDto.getPmsSkuStockList());
        cmsSubjectProductRelationApi.update(pmsProductDto.getSubjectProductRelationList());
    }

    //根据商品id获取商品编辑信息
    public PmsProductVo updateInfo(Integer id) {
        PmsProductVo pmsProductVo = new PmsProductVo();
        PmsProduct pmsProduct = pmsProductApi.selectById(id);
        BeanUtil.copyProperties(pmsProduct, pmsProductVo);
        List<PmsMemberPrice> pmsMemberPrice = pmsMemberPriceApi.selectById(id);
        pmsProductVo.setMemberPriceList(pmsMemberPrice);
        List<CmsPrefrenceAreaProductRelation> cpapr = cmsPrefrenceAreaProductRelationApi.selectById(id);
        pmsProductVo.setPrefrenceAreaProductRelationList(cpapr);
        List<PmsProductAttributeValue> pmsProductAttributeValues = pmsProductAttributeValueApi.selectById(id);
        pmsProductVo.setProductAttributeValueList(pmsProductAttributeValues);
        List<PmsProductFullReduction> productFullReductionList = pmsProductFullReductionApi.selectById(id);
        pmsProductVo.setProductFullReductionList(productFullReductionList);
        List<PmsProductLadder> productLadderList = pmsProductLadderApi.selectById(id);
        pmsProductVo.setProductLadderList(productLadderList);
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockApi.SelectById(id);
        pmsProductVo.setPmsSkuStockList(pmsSkuStockList);
        List<CmsSubjectProductRelation> cmsSubjectProductRelations = cmsSubjectProductRelationApi.selectById(id);
        pmsProductVo.setSubjectProductRelationList(cmsSubjectProductRelations);
        return pmsProductVo;
    }

}
